﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using MatchResults.Model;
using SportInfoService.Helpers;
using MatchResults.ViewModel;

namespace MatchResults.Services
{
    public class MatchProgramsService : ServiceBase<MatchProgramsViewModel>
    {
        protected override Uri GetUri(object param)
        {
            string MatchResultUrl = "http://www.iddaa.com.tr/XML/IDDAAMACPROGRAMI/index.htm?iddaadrawid={0}&foraccess=KSsec654";
            return new Uri(string.Format(MatchResultUrl, param));
        }

        protected override IEnumerable<MatchProgramsViewModel> ParseData(string data, object param)
        {
            data = data.Substring(data.IndexOf("<Draw"));
            data = data.Substring(0, data.IndexOf("<!--"));
            Draw draw = XMLHelper.Deserialize<Draw>(data, Encoding.UTF8);

            List<MatchProgramsViewModel> result = new List<MatchProgramsViewModel>();
            if (draw.League != null)
            {
                for (int i = 0; i < draw.League.Count; i++)
                {
                    result.Add(new MatchProgramsViewModel
                    {
                        Code = draw.League[i].Match[0].code,
                        League = draw.League[i].league,
                        LeagueName = draw.League[i].leagueName,
                        MatchDate = draw.League[i].Match[0].date,
                        MatchName = draw.League[i].Match[0].name,
                    });
                }
            }
            if (OnComplete != null)
            {
                OnComplete(this, new EventArgs());
            }

            return result;
        }

        protected override void AddWebClientHeaders(WebClient wc)
        {
            wc.Headers["Accept"] = "*/*";
            wc.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.3";
            wc.Headers["Accept-Encoding"] = "gzip,deflate,sdch";
            wc.Headers["Accept-Language"] = "en-US,en;q=0.8";
            wc.Headers["Cache-Control"] = "no-cache";
            wc.Headers["Host"] = "www.iddaa.com.tr";
            wc.Headers["Referer"] = "http://www.iddaa.com.tr/BITENMACLAR/FUTBOL/index.htm?PROGRAMID=1";
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5";
            wc.Encoding = Encoding.UTF8;
        }

        public override event EventHandler OnComplete;
    }
}
